{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Utils Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook contains examples of how to use the utility commands in the `advsecurenet` package on Command Line Interface (CLI)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils --help"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configs\n",
    "\n",
    "`advsecurenet` provides a way to configure the attack, defense and evaluation parameters using a configuration file. The configuration file is a YAML file that contains the parameters for the attack, defense and evaluation. Using the configuration file, you can easily change the parameters without changing the code or the command line arguments. Also it is easy to share the configuration file with others, reproduce the results and compare the results with different parameters without dealing with bunch of command line arguments. Such configuration files can be obtained from the `configs` command.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils configs --help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils configs list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils configs get --help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# To print the configuration file to the console\n",
    "!advsecurenet utils configs get -c fgsm_attack_config.yml -p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can also save the configuration file to a specific location\n",
    "!advsecurenet utils configs get -c fgsm_attack_config.yml -s -o ./test_fgsm_attack_config.yml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Models \n",
    "\n",
    "`advsecurenet` supports both PyTorch models, Custom models and External models. The PyTorch models are the models that are available in the `torchvision.models` module. The Custom models are the models that are defined in the package itself which are not available in the `torchvision.models` module. The External models are the models that can be provided by the user. `models` command provides a way to get the list of available models in the package and their layers. Layers are important for some attacks in the package like `LOTS`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils models --help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils models list -m standard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils models list -m custom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# if no model type is specified, the default is all\n",
    "!advsecurenet utils models list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils models layers -m resnet18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils models layers -m CustomMnistModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# It is also possible to inspect the model architecture with a normalization layer\n",
    "!advsecurenet utils models layers -m CustomMnistModel -n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Normalization Parameters\n",
    "\n",
    "You can easily get the normalization parameters for the well-known datasets like CIFAR-10, CIFAR-100, MNIST, FashionMNIST, SVHN, ImageNet using the `normalization` command. You can later use these normalization parameters in the configuration file while defining the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils normalization list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils normalization get -d imagenet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils normalization get -d fashion_mnist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Weights\n",
    "\n",
    "It is possible to download the weights of the models using the `weights` command. The weights are available for the PyTorch models, Custom models. For the standard PyTorch models, the weights are downloaded from the `torchvision.models` module. For the Custom models, the weights are downloaded from the package itself if available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils weights --help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils weights list -m resnet18"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "adv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.1.-1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
